import { getPodEvent } from "@/api/topoNodeDetail";
import { ref, watch } from "vue";
const sortModeMap = {
  desc: "descend",
  asc: "ascend",
  "": "",
};
export default function usePodEvent({node:nodeInfo, queryTime}) {
  const podEventParams = ref({
    key: "",
    pageNo: 1,
    pageSize: 10,
    sortMode: "desc",
    sortField: "eventTime",
  });
  const podEventTotal = ref(0);
  const podEventList = ref([]);

   // 获取POD事件列表
   const getPodEventData = () => {
    // const node = nodeInfo.value

    const { from, to} = queryTime.value
    const node = nodeInfo.value
    getPodEvent({
      ...podEventParams.value,
      from,
      to,
      masterIp: node.masterIp,
      namespace: node.namespace,
      podName: node.name,
    }).then((res) => {
      const { records, total } = res.data;
      // setPodEventTotal(total > 10000 ? 10000 : total);
      podEventTotal.value = total > 10000 ? 10000 : total
      // setPodEventList(records);
      podEventList.value = records
    });
  };
  const searchPodEvent = (value) => {
    // setPodEventParams({ ...podEventParams, key: value });
    podEventParams.value = { ...podEventParams.value, key:value}
  };
    // POD事件列表分页等操作
  const changeTable = (pagination, filters, sorter) => {
    const params = podEventParams.value;
    if (sorter.field) {
      params.sortField = sorter.field ? sorter.field : "";
      switch (sorter.order) {
        case "descend":
          params.sortMode = "desc";
          break;
        case "ascend":
          params.sortMode = "asc";
          break;
        default:
          params.sortMode = "";
          break;
      }
    }
    const { current, pageSize: size } = pagination;
    console.log(current, 'current');

    if (current !== params.pageNo) {
      params.pageNo = current;
    }
    if (size !== params.pageSize) {
      params.pageSize = size;
    }
  }
  watch(podEventParams, () => {
    if(node.type === 'pod') {
      getPodEventData()
    }
  })
  const podEventColumns = [
    {
      title: "事件原因",
      dataIndex: "reason",
    },
    {
      title: "事件详情",
      dataIndex: "message",

    },
    {
      title: "事件时间",
      dataIndex: "eventTime",
      sorter: true,
      sortOrder: sortModeMap[podEventParams.value.sortMode],
      render: (value) => moment(value).format("YYYY-MM-DD HH:mm:ss"),
    },
  ];
  return {
    podEventList,
    podEventTotal,
    podEventColumns,
    searchPodEvent,
    changeTable,
    getPodEventData
  }
}
